package com.sleep.statemachine.handle;

import com.sleep.statemachine.domain.Events;
import com.sleep.statemachine.domain.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

/**
 * 处理状态并开启状态机
 */
@Slf4j
@Component
@WithStateMachine(name = "OrderStateMachine")
public class EventListener {

    @OnTransition(target = "UNPAID") // 未支付状态 标记状态
    public void creat(Message<Events> message){
        log.info("创建订单！！！，等待付款");
    }

    @OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
    public Order pay(Message<Events> message){
        Order order = message.getHeaders().get("order", Order.class);
        log.info("支付成功！！！，等待收货   订单id：{}",order.getId());
        return order;
    }

    @OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
    public void receive(Message<Events> message){
        Order order = message.getHeaders().get("order", Order.class);
        log.info("收货成功！！！，订单完成   订单id：{}",order.getId());
    }

}
